#include "anim.h"

//Constructors
Animation::Animation(unsigned char duration)
{
	// Initialize at given duration
	max = duration;
	counter = 0;

	/* Positive being 0 means the animation is at a "standby" phase and 
	 * can't run yet
	 */
	positive = 0;
}

Animation::Animation()
{
	// Initialize at 0
	max = 0;
	counter = 0;
	positive = 0;
}

// Update
bool Animation::update()
{
	//Positive animation
	if (positive)
	{
		//Counter is below max
		if (counter < max)
		{
			//Advance animation and return updated
			counter++;
			return 1;
		}
	}
	//Negative animation
	else
	{
		//Counter is above zero
		if (counter > 0)
		{
			//Advance animation (negatively) and return updated
			counter--;
			return 1;
		}
	}

	//Animation is not updated
	return 0;
}

//Actions
void Animation::startFromStart() {counter = 0  ; positive = 1;}
void Animation::startFromEnd  () {counter = max; positive = 0;}
void Animation::startFromStart(unsigned char gMax) {max = gMax; counter = 0  ; positive = 1;}
void Animation::startFromEnd  (unsigned char gMax) {max = gMax; counter = max; positive = 0;}

//Isers
bool Animation::isAtStart() {return counter == 0   && !positive;}
bool Animation::isAtEnd  () {return counter == max &&  positive;}
bool Animation::isRunning() 
{
	return (counter < max &&  positive)
		|| (counter > 0   && !positive);
}

//Interpolation
float Animation::interpolate(float amount) {return amount *counter /max;}
